package me.zhengjie.modules.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

public class MyHttpUtils {

    //public static void main(String[] args) {
    //    // token可以从 数据中心 -> 我的接口 中获取
    //    String token = "ad6aec85-3fb7-4d55-94d7-6a57299b8bb4";
    //    String url = "http://open.api.tianyancha.com/services/v4/open/description?name=苍南县博浪科技发展有限公司&humanName=杨淑藕";
    //    System.out.println(executeGet(url, token));
    //}

    public static String GetJson(String token,String url){
        return executeGet(url, token);
    }

    protected static String executeGet(String url, String token) {
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 50000);
        HttpConnectionParams.setSoTimeout(httpParams, 10000);
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        String result = null;
        try {
            HttpGet get = new HttpGet(url);
            // 设置header
            get.setHeader("Authorization",token);
            // 设置类型
            HttpResponse response = httpClient.execute(get);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "utf-8");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return result;
    }
}
